package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.SpecificationOptionMapper;
import com.bwie.pojo.Specification;
import com.bwie.pojo.SpecificationOption;
import com.bwie.service.SpecificationOptionService;
import com.bwie.service.SpecificationService;
import com.bwie.mapper.SpecificationMapper;
import com.bwie.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 61630
* @description 针对表【tb_specification】的数据库操作Service实现
* @createDate 2024-08-15 16:20:03
*/
@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper, Specification>
    implements SpecificationService{
    @Resource
    private SpecificationOptionService specificationOptionService;
    @Resource
    private SpecificationMapper specificationMapper;
    @Resource
    private SpecificationOptionMapper specificationOptionMapper;
    @Transactional
    @Override
    public R add(Specification specification) {
        this.save(specification);
        for (SpecificationOption specificationOption : specification.getSpecificationOptionList()) {
            specificationOption.setSpecId(specification.getId());
        }
        this.specificationOptionService.saveBatch(specification.getSpecificationOptionList());
        return R.success();
    }

    @Override
    public R updates(Specification specification) {
        this.updateById(specification);
        LambdaQueryWrapper<SpecificationOption> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpecificationOption::getSpecId,specification.getId());
        this.specificationOptionService.remove(lambdaQueryWrapper);
        for (SpecificationOption specificationOption : specification.getSpecificationOptionList()) {
            specificationOption.setSpecId(specification.getId());
        }
        this.specificationOptionService.saveBatch(specification.getSpecificationOptionList());
        return R.success();
    }

    @Override
    public R del(Long id) {
        LambdaQueryWrapper<SpecificationOption> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpecificationOption::getSpecId,id);
        specificationOptionService.remove(lambdaQueryWrapper);
        return R.success();
    }

    @Override
    public R delByBath(List<Integer> idsList) {
        for (Integer id : idsList) {
            LambdaQueryWrapper<SpecificationOption> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SpecificationOption::getSpecId,id);
            specificationOptionService.remove(queryWrapper);
        }
        specificationMapper.deleteBatchIds(idsList);
        return null;
    }
}




